package com.qh.basic.service.impl;

import com.qh.basic.mapper.BaseMapper;
import com.qh.basic.query.BaseQuery;
import com.qh.basic.service.BaseService;
import com.qh.basic.util.pageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：基础
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@Service
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T loadOne(Long id) {
        return baseMapper.loadOne(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public pageList pageQuery(BaseQuery baseQuery) {
        //1.查询数据的总条数
        Integer count = baseMapper.queryCount(baseQuery);
        if (count<0){//没有查询到数据的情况下，直接返回
            return new pageList<>();
        }
        //2.查询当前页的数据
        List<T> date = baseMapper.queryDate(baseQuery);
        return new pageList<T>(count,date);
    }

    @Override
    public void patchDel(List<Long> ids) {
        baseMapper.patchDel(ids);
    }
}
